package a.a012;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.swa99ylum.pxf.PluggableXSSFFiller;
import com.swa99ylum.pxf.cell.CellFiller;
import com.swa99ylum.pxf.cell.adhesive.Adhesive;
import com.swa99ylum.pxf.cell.styler.CellStyler;
import com.swa99ylum.pxf.row.RowFiller;
import com.swa99ylum.pxf.sheet.SheetFiller;

public class Test1 {
	
	
	public static void main(String[] args) throws Exception {
		
		
		File file = new File("src/main/java/a/a012/test1.xlsx");
		System.out.println(file.getAbsolutePath());
		
		
		FileInputStream fis = new FileInputStream(file);
		XSSFWorkbook xwb = new XSSFWorkbook(fis);
		
		/** ===================================================== */
		
		/** 正式使用 PluggableXSSFFiller */
		
		List<Student> sss = new ArrayList<>();
		
		
		sss.add(new Student(123, "三（1）班", "张三",	98, "上海", new Date()));
		sss.add(new Student(124, "三（1）班", "李四",	75, "上海", new Date()));
		sss.add(new Student(125, "三（1）班", "王五",	89, "四川", new Date()));
		sss.add(new Student(126, "三（2）班", "赵六",	88, "西安", new Date()));
		sss.add(new Student(127, "三（2）班", "阿斯顿", 	75, "重庆", new Date()));
		sss.add(new Student(128, "三（2）班", "发的",	56, "重庆", new Date()));
		sss.add(new Student(129, "三（2）班", "揉一揉", 	105, "北京", new Date()));
		sss.add(new Student(130, "三（2）班", "线程", 	89, "北京", new Date()));
		sss.add(new Student(131, "三（3）班", "欧井看", 	99, "杭州", new Date()));
		sss.add(new Student(132, "三（3）班", "魄力", 	76, "盐城", new Date()));
		sss.add(new Student(133, "三（3）班", "刺耳", 	65, "扬州", new Date()));
		sss.add(new Student(134, "三（4）班", "你好", 	68, "天津", new Date()));
		sss.add(new Student(135, "三（4）班", "让他改", 	97, "天津", new Date()));
		sss.add(new Student(136, "三（5）班", "哦平", 	82, "常州", new Date()));
		
//		List<Student> sss2 = new ArrayList<>();
//		sss2.add(new Student(441, "aaa", "上海", new Date(), "zzz"));
//		sss2.add(new Student(442, "aaa123", "北京", new Date(), "zzxc"));
//		sss2.add(new Student(443, "bbb", "四川", new Date(), "ttt"));
//		sss2.add(new Student(444, "bbb456", "西安", new Date(), "qqq"));
		
		
		new PluggableXSSFFiller(xwb, new SheetFiller[] {
				new SheetFiller(new String[]{"Sheet1", "aaa"}, new RowFiller[] {
						new RowFiller(6, sss, new CellFiller[] {
								new CellFiller(0, new Object[]{"@{clazz}", "@{clazz}", "@{clazz}"}, new CellStyler() {
									
									@Override
									public void doStyle(XSSFCell cell, Object cellValue) {
										
										/** CellStyle 是 引用类型的，所以下面 2 行执行完后，只有 B6 生效了，并不是 A6+B6 */
//										cell.setCellStyle(getStyle("A6"));
//										cell.setCellStyle(getStyle("B6"));
										
										/** 解决：在 styleSheet 中 将 2 种样式 合并在一个 新的 cell 上 */
										cell.setCellStyle(getStyle("C6"));
										
									}
								}, new Adhesive()),
								new CellFiller(null, new Object[]{"@{id}", "@{name}", "分数："}),
								new CellFiller(null, new Object[]{"@{score}"}, new CellStyler() {
									
									@Override
									public void doStyle(XSSFCell cell, Object cellValue) {
										
										int score = (int) cellValue;
										
										//System.out.println("score ==>" + score);
										
										if(90<=score && score<=100) {
											cell.setCellStyle(getStyle("A5"));
											
										}else if(80<=score && score<=89) {
											cell.setCellStyle(getStyle("B5"));
											
										}else if(70<=score && score<=79) {
											cell.setCellStyle(getStyle("C5"));
											
										}else if(60<=score && score<=69) {
											cell.setCellStyle(getStyle("D5"));
											
										}else if(00<=score && score<=59) {
											cell.setCellStyle(getStyle("E5"));
											
										}else {
											cell.setCellStyle(getStyle("F5"));
										}
										
									}
								}),
								new CellFiller(null, new Object[]{"@{address}", "@{address}"}, new Adhesive(new Object[]{}, null)),
								new CellFiller(null, new Object[]{"@{birthDay}"}, "A2"),
						}),
				}),
				
		}, "MY_STYLE")
		.fill();
		
		/** ===================================================== */
		
		FileOutputStream fo = new FileOutputStream(file);
		xwb.write(fo);
		fo.close();
		
	}


}
